package edu.imac.angryfood.component;

import java.util.ArrayList;

import javax.swing.Timer;

import org.jbox2d.common.Vec2;
import org.newdawn.slick.SlickException;

import edu.imac.angryfood.world.World;

/** A projectile that is divided in 3 when the user clicks and it is still in the air */

public class DividedProjectile extends AbstractProjectile {
	
	/** the images of the fragments */
	private ArrayList<String> imagesMini;
	/** the width of the fragments */
	private int widthMini;
	/** the height of the fragments */
	private int heightMini;
	
	/** constructor for circle projectiles
	 * 
	 * @param x x position of the projectile
	 * @param y y position of the projectile
	 * @param radius radius of the projectile
	 * @param droped if the projectile has already been droped
	 * @param num the order in which the projectile will be launched
	 * @param image the name of the image's file
	 * @param imagesMini list of the names of the images's file of the divisions
	 */
	
	public DividedProjectile(int x, int y, float radius, boolean droped, int num, String image, ArrayList<String>imagesMini)
		throws SlickException {
	super(x, y, radius, droped, num, image);
		this.imagesMini = imagesMini;
	}
	
	/** constructor for rectangle projectiles
	 * 
	 * @param x x position of the projectile
	 * @param y y position of the projectile
	 * @param width width of the projectile
	 * @param height height of the projectile
	 * @param droped if the projectile has already been droped
	 * @param num the order in which the projectile will be launched
	 * @param image the name of the image's file
	 */
	
	public DividedProjectile(int x, int y, int width, int height, boolean droped, int num, String image, int widthMini, int heightMini, ArrayList<String>imagesMini)
		throws SlickException {
	super(x, y, width, height, droped, num, image);
		this.widthMini = widthMini;
		this.heightMini = heightMini;
		this.imagesMini = imagesMini;
	}
	
	public ArrayList<String> getImagesMini() {
		return imagesMini;
	}

	public int getWidthMini() {
		return widthMini;
	}

	public int getHeightMini() {
		return heightMini;
	}

	/** when the user clicks, the projectile is divided in three fragments
	 * 
	 * @param world the world that contains the game
	 * @throws SlickException
	 */
	
	@Override
	public void click(World world) throws SlickException {
		if (tLaunched instanceof Timer){
			tLaunched.stop();
		}
		clicked=true;
		next(world);
		Vec2 boom = new Vec2();
		Vec2 center = new Vec2();
		Vec2 velocity=getLinearVelocity();
		double teta;
		//3 projectiles are created :
		//1 which keep the same direction
		//2 that are taking off pi/6
		//a rotation matrix is applied to the velocity vector
		for (int i = -1; i <= 1; i++) {
			teta = i * 3.14 / 6;
			boom.x = (float) (velocity.x*Math.cos(teta)-velocity.y*Math.sin(teta));
			boom.y = (float) (velocity.x*Math.sin(teta)+velocity.y*Math.cos(teta));
			center.x = (float) (getJboxBody().getWorldCenter().x + (radius)
					* Math.cos(teta));
			center.y = (float) (getJboxBody().getWorldCenter().y + (radius)
					* Math.sin(teta));
			AbstractProjectile proj = null;
			if(shapeType.equals("Circle"))
				proj = new SimpleProjectile((int) center.x, (int) center.y,5,true, -1, imagesMini.get(i+1));
			else
				proj = new SimpleProjectile((int) center.x, (int) center.y, widthMini, heightMini,true, -1, imagesMini.get(i+1));
			proj.setClicked(true);
			world.add(proj);
			proj.getJBoxBody().setLinearVelocity(boom);
			
		}
		world.remove(this);

	}

	/** when the projectile collides something, it cannot be clicked anymore
	 * 
	 * @param world the world that contains the game
	 */
	@Override
	public void collision(World world) {
		if (!collided){
			collided=true;
			if (tLaunched instanceof Timer){
				tLaunched.stop();
			}
			clicked=true;
			next(world);	
		}
	}

}
